package com.yanqu.road.logic.pb;

import com.yanqu.road.entity.activity.unionwar.UnionWarDispatchDetail;
import com.yanqu.road.entity.activity.unionwar.UnionWarUserPatrons;
import com.yanqu.road.entity.activity.unionwar.UnionWarUserPatronsDetail;
import com.yanqu.road.entity.activity.unionwar.UnionWarUserSilver;
import com.yanqu.road.entity.activity.unionwar.cross.CrossUnionWarDetail;
import com.yanqu.road.entity.activity.unionwar.cross.UnionWarPatronsData;
import com.yanqu.road.entity.activity.unionwar.cross.UnionWarUserData;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.union.UnionBaseInfo;
import com.yanqu.road.entity.unionwar.GuessUnionInfo;
import com.yanqu.road.logic.helper.UnionHelper;
import com.yanqu.road.pb.activity.unionwar.CrossUnionWarProto;
import com.yanqu.road.pb.activity.unionwar.UnionWarActivityServerProto;

import java.math.BigInteger;
import java.util.*;

/**
 * 群英争霸
 */
public class UnionWarActivityPb {

    /**
     * 阵容
     * @param list
     * @param detailInfo 包含门客详情
     * @return
     */
    public static List<UnionWarActivityServerProto.SessionLineupTemp.Builder> parseSessionLineupTempList(List<UnionWarDispatchDetail> list, boolean detailInfo, int memeberCount, UnionBaseInfo enemyUnionInfo, UnionBaseInfo unionInfo) {
        List<UnionWarActivityServerProto.SessionLineupTemp.Builder> resultList = new ArrayList<>();
        int size = list.size();
        for (int i = 0; i < size; i++) {
            UnionWarDispatchDetail detail = list.get(i);
            UnionWarActivityServerProto.SessionLineupTemp.Builder builder = parseSessionLineupTempList(detail, detailInfo, memeberCount, enemyUnionInfo, unionInfo);
            resultList.add(builder);
        }
        return resultList;
    }

    /**
     * 阵容
     * @param detail
     * @param detailInfo 包含门客详情
     * @param memberCount
     * @return
     */
    public static UnionWarActivityServerProto.SessionLineupTemp.Builder parseSessionLineupTempList(UnionWarDispatchDetail detail, boolean detailInfo, int memberCount, UnionBaseInfo enemyUnionInfo, UnionBaseInfo unionInfo) {
        UnionWarActivityServerProto.SessionLineupTemp.Builder builder = UnionWarActivityServerProto.SessionLineupTemp.newBuilder();
        builder.setSession(detail.getSession());
        int num = 0;
        for(UnionWarUserPatrons teamUserPatrons : detail.getPatronsMap().values()){
            if(teamUserPatrons.getPatrons().size() > 0) { //有门客
                num = num + 1;
            }
        }

        builder.setUserNum(num);
        if(detail.getEnemyUnionUid() != null) {
            builder.setEnemyUnionUid(detail.getEnemyUnionUid());
        }
        if(enemyUnionInfo != null) {
            if(detailInfo) {
                builder.setEnemyUnionInfo(UnionBasePb.parseUnionBaseTempMsg(enemyUnionInfo));
            }else {
                builder.setEnemyUnionInfo(UnionBasePb.parseUnionBaseTempMsg(enemyUnionInfo));
                builder.setEnemyUnionName(enemyUnionInfo.getUnionName());
                builder.addAllEnemyUnionFlag(UnionHelper.getUnionFlag(enemyUnionInfo));
            }
        }
        builder.setUnionInfo(UnionBasePb.parseUnionBaseTempMsg(unionInfo));
        builder.setUnionUid(detail.getUnionUid());
        builder.setMemberCount(memberCount);
        builder.setWinMark(detail.getWinMark());
        builder.setPhase(detail.getPhase());
        long totalAbility = 0l;
        for(Map.Entry<Long, UnionWarUserPatrons> entry : detail.getPatronsMap().entrySet()){
            for(Map.Entry<Integer, UnionWarUserPatronsDetail> detailEntry : entry.getValue().getPatrons().entrySet()){
                UserBaseInfo userBaseInfo = detail.getUserBaseInfoMap().get(entry.getKey());
                totalAbility += detailEntry.getValue().getAbility2();
                if(detailInfo) {
                    //门客
                    UnionWarActivityServerProto.UnionWarUserPatronsTemp.Builder patronsTemp = parseUnionWarUserPatronsTemp(detailEntry.getValue(), entry.getKey(), userBaseInfo == null ? "" : userBaseInfo.getNickName(), entry.getValue().getPosition());
                    patronsTemp.setSendTime(entry.getValue().getSendTime());
                    builder.addPatronsList(patronsTemp);
                }
            }
            if(detailInfo) {
                //锦囊
                builder.addGoodsList(parseUnionWarUserGoodsTemp(entry.getValue().getGoodsId(), entry.getKey()));
            }
        }
        builder.setTotalAbility(totalAbility);

        BigInteger totalSilver = BigInteger.ZERO;
        //银两
        for(Map.Entry<Long, UnionWarUserSilver> entry : detail.getSilverMap().entrySet()){
            totalSilver = totalSilver.add(entry.getValue().getSilver());
            if(detailInfo) {
                builder.addSilverList(parseUnionWarUserSilverTemp(entry.getValue(), entry.getKey()));
            }
        }
        builder.setTotalSilver(totalSilver.toString());
        builder.addAllReadUserId(detail.getReadUserId());
        return builder;
    }

    public static UnionWarActivityServerProto.SessionLineupTemp.Builder parseSessionLineupTempList(CrossUnionWarDetail detail, boolean detailInfo, int memberCount, UnionBaseInfo enemyUnionInfo, UnionBaseInfo unionInfo) {
        UnionWarActivityServerProto.SessionLineupTemp.Builder builder = UnionWarActivityServerProto.SessionLineupTemp.newBuilder();
        builder.setSession(detail.getRoundNo());
        Set<Long> userIdSet = new HashSet<>();
        for (int i = 0; i < detail.getPatronsDataList().size(); i++) {
            UnionWarPatronsData patronsData = detail.getPatronsDataList().get(i);
            userIdSet.add(patronsData.getUserId());
        }

        builder.setUserNum(userIdSet.size());
        if(detail.getEnemyUnionUid() != null) {
            builder.setEnemyUnionUid(detail.getEnemyUnionUid());
        }
        if(enemyUnionInfo != null) {
            if(detailInfo) {
                builder.setEnemyUnionInfo(UnionBasePb.parseUnionBaseTempMsg(enemyUnionInfo));
            }else {
                builder.setEnemyUnionInfo(UnionBasePb.parseUnionBaseTempMsg(enemyUnionInfo));
                builder.setEnemyUnionName(enemyUnionInfo.getUnionName());
                builder.addAllEnemyUnionFlag(UnionHelper.getUnionFlag(enemyUnionInfo));
            }
        }
        builder.setUnionInfo(UnionBasePb.parseUnionBaseTempMsg(unionInfo));
        builder.setUnionUid(detail.getUnionUid());
        builder.setMemberCount(memberCount);
        builder.setWinMark(detail.getWinMark());
        builder.setPhase(detail.getStageNo());
        long totalAbility = 0l;
        for (int i = 0; i < detail.getPatronsDataList().size(); i++) {
            UnionWarPatronsData patronsData = detail.getPatronsDataList().get(i);
            totalAbility += patronsData.getAbility2().longValue();
            if(detailInfo) {
                UnionWarUserData userData = detail.getUserDataMap().get(patronsData.getUserId());
                UserBaseInfo userBaseInfo = userData.getUserBaseInfo();
                //门客
                UnionWarActivityServerProto.UnionWarUserPatronsTemp.Builder patronsTemp = parseUnionWarUserPatronsTemp(patronsData, userData.getUserId(), userBaseInfo == null ? "" : userBaseInfo.getNickName(), userData.getPosition());
                patronsTemp.setSendTime(patronsData.getModifyTime());
                builder.addPatronsList(patronsTemp);
                //锦囊
                builder.addGoodsList(parseUnionWarUserGoodsTemp(userData.getUseItemId(), userData.getUserId()));
            }
        }
        builder.setTotalAbility(totalAbility);

        BigInteger totalSilver = BigInteger.ZERO;
        //银两
        for (Map.Entry<Long, UnionWarUserData> entry : detail.getUserDataMap().entrySet()) {

//        }
//        for(Map.Entry<Long, UnionWarUserSilver> entry : detail.getSilverMap().entrySet()){
            totalSilver = totalSilver.add(entry.getValue().getDonateSilver());
            if(detailInfo) {
                builder.addSilverList(parseUnionWarUserSilverTemp(entry.getValue(), entry.getKey()));
            }
        }
        builder.setTotalSilver(totalSilver.toString());
        builder.addAllReadUserId(detail.getReadUserId());
        return builder;
    }


    //银两
    public static UnionWarActivityServerProto.UnionWarUserSilverTemp.Builder parseUnionWarUserSilverTemp(UnionWarUserSilver silver, long userId){
        UnionWarActivityServerProto.UnionWarUserSilverTemp.Builder builder = UnionWarActivityServerProto.UnionWarUserSilverTemp.newBuilder();
        builder.setSilver(silver.getSilver().toString());
        builder.setUserId(userId);
        builder.setPosition(silver.getPosition());
        builder.setNickName(silver.getNickName());
        return builder;
    }

    //银两
    public static UnionWarActivityServerProto.UnionWarUserSilverTemp.Builder parseUnionWarUserSilverTemp(UnionWarUserData silver, long userId){
        UnionWarActivityServerProto.UnionWarUserSilverTemp.Builder builder = UnionWarActivityServerProto.UnionWarUserSilverTemp.newBuilder();
        builder.setSilver(silver.getDonateSilver().toString());
        builder.setUserId(userId);
        builder.setPosition(silver.getPosition());
        builder.setNickName(silver.getUserBaseInfo().getNickName());
        return builder;
    }

    //锦囊
    public static UnionWarActivityServerProto.UnionWarUserGoodsTemp.Builder parseUnionWarUserGoodsTemp(int goodsId, long userId){
        UnionWarActivityServerProto.UnionWarUserGoodsTemp.Builder builder = UnionWarActivityServerProto.UnionWarUserGoodsTemp.newBuilder();
        builder.setGoodsId(goodsId);
        builder.setUserId(userId);
        return builder;
    }


    //门客
    public static UnionWarActivityServerProto.UnionWarUserPatronsTemp.Builder parseUnionWarUserPatronsTemp(UnionWarPatronsData detail, long userId, String userName, int position){
        UnionWarActivityServerProto.UnionWarUserPatronsTemp.Builder builder = UnionWarActivityServerProto.UnionWarUserPatronsTemp.newBuilder();
        builder.setUserId(userId);
        builder.setAbility(detail.getAbility2().longValue());
        builder.setPatronsId(detail.getPatronsId());
        builder.setPosition(position);
        builder.setType(detail.getAmbush());
        builder.setSkillId(detail.getSkinId());
        builder.setUserName(userName);
        builder.setSkillAddition(detail.getSkillAddition());
        builder.setPromotionId(detail.getPromotionId());
        return builder;
    }

    //门客
    public static UnionWarActivityServerProto.UnionWarUserPatronsTemp.Builder parseUnionWarUserPatronsTemp(UnionWarUserPatronsDetail detail, long userId, String userName, int position){
        UnionWarActivityServerProto.UnionWarUserPatronsTemp.Builder builder = UnionWarActivityServerProto.UnionWarUserPatronsTemp.newBuilder();
        builder.setUserId(userId);
        builder.setAbility(detail.getAbility2());
        builder.setPatronsId(detail.getPatronsId());
        builder.setPosition(position);
        builder.setType(detail.getType());
        builder.setSkillId(detail.getSkinId());
        builder.setUserName(userName);
        builder.setSkillAddition(detail.getSkillAddition());
        builder.setPromotionId(detail.getPromotionId());
        return builder;
    }

    private static List<UnionWarPatronsData> getPatronsData(List<UnionWarPatronsData> patronsDataList, long userId) {
        List<UnionWarPatronsData> patronsData = new ArrayList<>();
        for (UnionWarPatronsData unionWarPatronsData : patronsDataList) {
            if (unionWarPatronsData.getUserId() == userId) {
                patronsData.add(unionWarPatronsData);
            }
        }
        return patronsData;
    }

    /**
     * 备战数据
     * @param detail
     * @param serverId
     * @return
     */
    public static CrossUnionWarProto.CrossUnionWarUnionDetailTempMsg.Builder parseCrossUnionWarUnionDetailTempMsg(UnionWarDispatchDetail detail, long serverId) {
        CrossUnionWarProto.CrossUnionWarUnionDetailTempMsg.Builder detailTemp = CrossUnionWarProto.CrossUnionWarUnionDetailTempMsg.newBuilder();
        detailTemp.setUnionUid(detail.getUnionUid());
        detailTemp.addAllReadUserId(detail.getReadUserId());
        for(long userId : detail.getUserBaseInfoMap().keySet()){
            CrossUnionWarProto.CrossUnionWarUserDetailTempMsg.Builder userDetailTem = CrossUnionWarProto.CrossUnionWarUserDetailTempMsg.newBuilder();
            UserBaseInfo userBaseInfo = detail.getUserBaseInfoMap().get(userId);
            if(userBaseInfo != null){
                if (userBaseInfo.getServerId() != serverId) {
                    continue;
                }
                userDetailTem.setUserBaseInfo(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo));
                userDetailTem.setServerId(userBaseInfo.getServerId());
            } else {
                continue;
            }
            userDetailTem.setUserId(userId);
            UnionWarUserPatrons userPatrons = detail.getPatronsMap().get(userId);
            if(userPatrons != null){
                userDetailTem.setUseItemId(userPatrons.getGoodsId());
                userDetailTem.setUnionPosition(userPatrons.getPosition());
                userDetailTem.setSendTime(userPatrons.getSendTime());
                //门客
                for(UnionWarUserPatronsDetail patronsDetail : userPatrons.getPatrons().values()){
                    CrossUnionWarProto.CrossUnionWarPatronsTempMsg.Builder patronsTemp = CrossUnionWarProto.CrossUnionWarPatronsTempMsg.newBuilder();
                    patronsTemp.setAbility(String.valueOf(patronsDetail.getAbility()));
                    patronsTemp.setPatronsId(patronsDetail.getPatronsId());
                    patronsTemp.setPatronsLv(patronsDetail.getPatronsLv());
                    patronsTemp.setPatronsStageLv(patronsDetail.getPatronsStageLv());
                    patronsTemp.setAmbush(patronsDetail.getType());
                    patronsTemp.setSkinId(patronsDetail.getSkinId());
                    patronsTemp.setSkillAddition(patronsDetail.getSkillAddition());
                    patronsTemp.setLianShengAddition(patronsDetail.getLianShengAddition());
                    patronsTemp.setAbility2(String.valueOf(patronsDetail.getAbility2()));
                    patronsTemp.setPromotionId(patronsDetail.getPromotionId());
                    userDetailTem.addPatronsDataList(patronsTemp);
                }
            }else{
                userDetailTem.setUseItemId(0);
            }
            UnionWarUserSilver userSilver = detail.getSilverMap().get(userId);
            if(userSilver != null){
                userDetailTem.setDonateSilver(String.valueOf(userSilver.getSilver()));
                userDetailTem.setIsQuitUnion(userSilver.isQuit());
                if(!userDetailTem.hasUnionPosition()) {
                    userDetailTem.setUnionPosition(userSilver.getPosition());
                }
            }else {
                userDetailTem.setDonateSilver("0");
                userDetailTem.setIsQuitUnion(false);
            }
            if(!userDetailTem.hasSendTime()){
                userDetailTem.setSendTime(0l);
            }
            detailTemp.addUserDataList(userDetailTem);
        }
        return detailTemp;
    }

    /**
     * 竞猜信息
     * @param guessUnionInfo
     * @return
     */
    public static UnionWarActivityServerProto.UnionWarGuessTemp.Builder parseUnionWarGuessTemp(GuessUnionInfo guessUnionInfo) {
        UnionWarActivityServerProto.UnionWarGuessTemp.Builder temp = UnionWarActivityServerProto.UnionWarGuessTemp.newBuilder();
        temp.setUnionUid(guessUnionInfo.getUnionUid());
        temp.setUnionBaseTemp(UnionBasePb.parseUnionBaseTempMsg(guessUnionInfo.getUnionBaseInfo()));
        temp.setPopularity(guessUnionInfo.getGuessPopularity());
        return temp;
    }

    public static UnionWarActivityServerProto.CrossUnionWarDetailTemp.Builder buildCrossUnionWarDetailTemp(CrossUnionWarDetail detail) {
        UnionWarActivityServerProto.CrossUnionWarDetailTemp.Builder builder = UnionWarActivityServerProto.CrossUnionWarDetailTemp.newBuilder();
        builder.setStageNo(detail.getStageNo());
        builder.setRoundNo(detail.getRoundNo());
        builder.setUnionUid(detail.getUnionUid());
        builder.setEnemyUnionUid(detail.getEnemyUnionUid());
        builder.setModifyTime(detail.getModifyTime());
        builder.setWinMark(detail.getWinMark());
        builder.setGroupId(detail.getGroupId());

        for (UnionWarUserData warUserData : detail.getUserDataMap().values()) {
            CrossUnionWarProto.CrossUnionWarUserDetailTempMsg.Builder userDetail = CrossUnionWarProto.CrossUnionWarUserDetailTempMsg.newBuilder();
            userDetail.setUserId(warUserData.getUserId());
            List<UnionWarPatronsData> patronsData = getPatronsData(detail.getPatronsDataList(), warUserData.getUserId());
            if(patronsData != null){
                //门客
                for(UnionWarPatronsData patronsDetail : patronsData){
                    CrossUnionWarProto.CrossUnionWarPatronsTempMsg.Builder patronsTemp = CrossUnionWarProto.CrossUnionWarPatronsTempMsg.newBuilder();
                    patronsTemp.setAbility(String.valueOf(patronsDetail.getAbility()));
                    patronsTemp.setPatronsId(patronsDetail.getPatronsId());
                    patronsTemp.setPatronsLv(patronsDetail.getPatronsLv());
                    patronsTemp.setPatronsStageLv(patronsDetail.getPatronsStageLv());
                    patronsTemp.setAmbush(patronsDetail.getAmbush());
                    patronsTemp.setSkinId(patronsDetail.getSkinId());
                    patronsTemp.setSkillAddition(patronsDetail.getSkillAddition());
                    patronsTemp.setLianShengAddition(patronsDetail.getLianShengAddition());
                    patronsTemp.setPromotionId(patronsDetail.getPromotionId());
                    userDetail.addPatronsDataList(patronsTemp);
                }
            }else{
                userDetail.setUseItemId(0);
            }
            UserBaseInfo userBaseInfo = warUserData.getUserBaseInfo();
            if(userBaseInfo != null){
                userDetail.setUserBaseInfo(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo));
                userDetail.setServerId(userBaseInfo.getServerId());
            }
            userDetail.setDonateSilver(String.valueOf(warUserData.getDonateSilver()));
            userDetail.setIsQuitUnion(warUserData.isQuitUnion());
            userDetail.setUnionPosition(warUserData.getPosition());
            userDetail.setUseItemId(warUserData.getUseItemId());
            userDetail.setSendTime(warUserData.getModifyTime());
            if(!userDetail.hasSendTime()){
                userDetail.setSendTime(0l);
            }
            builder.addUserDataList(userDetail);
        }
        return builder;

    }

}
